package com.yueqian.sort;

import java.util.Arrays;

/**
 * @author shkstart
 * @create 2021-01-14 20:07
 */
public class ChanceSort_2 {
    public static void main(String[] args) {
        int[] a = new int[]{2, 5, 8, 1, 32, 14, 3, 31, 2, 1, 5, 3};
        sort(a);
        System.out.println(Arrays.toString(a));
    }

    public static void sort(int[] a) {
        sort(a, 0);
    }

    public static void sort(int[] a, int startIndex) {
        //获取起始位置的元素内容
        int minNum = a[startIndex];

        //记录最小元素位置
        int minIndex = startIndex;
        for (int i = startIndex + 1; i < a.length; i++) {
            if (a[i] < minNum) {
                minIndex = i;
                minNum = a[i];
            }
        }
        //交换位置
        int t = a[startIndex];
        a[startIndex] = a[minIndex];
        a[minIndex] = t;

        //判断是否还需要排序
        if (startIndex >= a.length - 2) {
            return;
        }


        //递归后续的元素排序
        sort(a, ++startIndex);


    }
}
